package com.cytech.cloudclinic.adapter;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.bean.CustomPrescriptionEntity;

import java.util.List;

import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_1;
import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_2;
import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_3;
import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_4;
import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_5;
import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_6;

public class PrescriptionParentAdapter extends BaseQuickAdapter<CustomPrescriptionEntity, BaseViewHolder> {

    private int currentPos = 0;
    private OnChildClickListener onChildClickListener;
    private boolean isShowPoint = false;
    private int parentDrugId;

    public PrescriptionParentAdapter(List<CustomPrescriptionEntity> data,boolean isShowPoint,int parentDrugId, OnChildClickListener onChildClickListener) {
        super(R.layout.item_prescription_select_drug, data);
        this.onChildClickListener = onChildClickListener;
        this.isShowPoint = isShowPoint;
        this.parentDrugId = parentDrugId;
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, CustomPrescriptionEntity s) {
        //服用天数,用法总布局
        LinearLayout dayLayout = baseViewHolder.getView(R.id.ll_item_day_number);
        //每日贴数
        LinearLayout dayTimesLayout = baseViewHolder.getView(R.id.ll_item_priscription_day_times);
        //用法
        LinearLayout userLayout = baseViewHolder.getView(R.id.ll_prescription_use_method);


        switch (parentDrugId){
            case DRUG_TAG_1:
                dayLayout.setVisibility(View.GONE);
                userLayout.setVisibility(View.GONE);
                break;
            case DRUG_TAG_2:
                dayLayout.setVisibility(View.VISIBLE);
                userLayout.setVisibility(View.GONE);
                dayTimesLayout.setVisibility(View.GONE);
                break;
            case DRUG_TAG_3:
                dayLayout.setVisibility(View.VISIBLE);
                userLayout.setVisibility(View.VISIBLE);
                break;
            case DRUG_TAG_4:
                dayLayout.setVisibility(View.VISIBLE);
                userLayout.setVisibility(View.GONE);
                break;
            case DRUG_TAG_5:
                dayLayout.setVisibility(View.GONE);
                userLayout.setVisibility(View.GONE);
                break;
            case DRUG_TAG_6:
                dayLayout.setVisibility(View.GONE);
                userLayout.setVisibility(View.GONE);
                break;
            default:
                break;
        }

        baseViewHolder.setText(R.id.item_doctor_order,s.doctorOrder);

        TextView groupTag = baseViewHolder.getView(R.id.tv_prescription_group_tag);
        RecyclerView childRv = baseViewHolder.getView(R.id.rv_parent_prescription_list);
        groupTag.setText(String.valueOf(baseViewHolder.getLayoutPosition() + 1));
        childRv.setLayoutManager(new LinearLayoutManager(getContext()));
        PrescriptionChildAdapter childAdapter = new PrescriptionChildAdapter(s.drugList,isShowPoint,parentDrugId);
        childRv.setAdapter(childAdapter);
        childAdapter.addChildClickViewIds(R.id.ll_item_acupoint, R.id.ll_item_acupoint_time,R.id.ll_item_drug_info,R.id.tv_prescription_child_drug_del,R.id.ll_item_acupoint_body);
        childAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                switch (view.getId()) {
                    //穴位
                    case R.id.ll_item_acupoint:
                        if (onChildClickListener != null) {
                            onChildClickListener.acupointClick(baseViewHolder.getLayoutPosition(), position);
                        }
                        break;
                    //时间
                    case R.id.ll_item_acupoint_time:
                        if (onChildClickListener != null) {
                            onChildClickListener.acupointTime(baseViewHolder.getLayoutPosition(), position);
                        }
                        break;
                    //药品
                    case R.id.ll_item_drug_info:
                        if (onChildClickListener != null) {
                            onChildClickListener.drugItemClick(baseViewHolder.getLayoutPosition(), position);
                        }
                        break;
                    case R.id.tv_prescription_child_drug_del:
                        childAdapter.remove(position);
                        break;
                    case R.id.ll_item_acupoint_body:   //体位
                        if (onChildClickListener != null) {
                            onChildClickListener.bodyPointClick(baseViewHolder.getLayoutPosition(), position);
                        }
                        break;
                }

            }
        });


        if (baseViewHolder.getAdapterPosition() == currentPos) {
            groupTag.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
            groupTag.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.shape_round_real_bg_all));
        } else {
            groupTag.setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
            groupTag.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.shape_round_empty_bg_all));
        }
    }

    public void setCurrentPos(int currentPos) {
        this.currentPos = currentPos;
        notifyDataSetChanged();
    }

    public interface OnChildClickListener {
        //穴位被点击
        void acupointClick(int parentPos, int childPos);

        //时间贴数被点击
        void acupointTime(int parentPos, int childPos);

        void drugItemClick(int parentPos, int childPos);
        //体位被点击
        void bodyPointClick(int parentPos, int childPos);
    }

}
